<template>
	<view class="container">
		<TopNav bg-color="#fff"></TopNav>

		<view class="p-l30 p-r30 m-t30">
			<view class="f-s30 text-bold">
				<text>输入短信验证码</text>
			</view>
			<view class="m-t22">
				<text class="text-gray p-r8">验证码已发送至</text>
				<text class="f-s16">{{formatPhoneNumber}}</text>
			</view>
			<view class="m-t40 p-t0">
				<u-code-input v-model="code" :focus="true" mode="line" maxlength="4" size="112rpx" space="60rpx"
					borderColor="#DDE0E6" :always-embed="true" :adjust-position="true" cursor-spacing="30" bold
					@finish="handleFinish"></u-code-input>
			</view>
			<view class="text-gray text-right m-t20" v-if="countdown>0">
				<text>{{countdown}}</text>
				<text>s后重新发送</text>
			</view>
			<view class="text-right m-t20" v-else @click="getCode" hover-class="pressed">
				<text>发送验证码</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getCode
	} from '@/api/login'
	import {
		checkAgreement,
		authDetails
	} from '@/api/mine/auth.js'
	export default {
		name: "",
		data() {
			return {
				code: '',
				countdown: 60,
				phoneNumber: '',
				clientId: '', // 推送cid
				userInfo: {}
			}
		},
		onLoad(query) {
			if (query.phonenumber) this.phoneNumber = query.phonenumber
			this.countdownStart()
			// #ifdef APP
			//只有在基座运行的情况下才能打印看到
			plus.push.getClientInfoAsync((info) => {
				this.clientId = info.clientid
				console.log('info: ',info);
			}, err => {
				console.log('clientInfo err: ', err);
			});
			// #endif
		},
		computed: {

			formatPhoneNumber() {
				// 确保手机号是一个字符串
				let str = this.phoneNumber.toString();
				// 使用正则表达式格式化
				return str.replace(/^(\d{3})(\d{4})(\d{4})$/, '\$1 \$2 \$3');
			}
		},

		methods: {

			handleFinish() {
				uni.hideKeyboard()
				this.$modal.loading('登录中').then(res => {
					let params = {
						mobile: this.phoneNumber,
						sms_code: this.code,
						client_ids: this.clientId
					}
					this.$store.dispatch('Login', params).then(() => {
						this.loginSuccess()
					}).catch(() => {
						if (this.captchaEnabled) {
							this.$modal.closeLoading()
						}
					})
				}).catch(err => {
					this.$modal.closeLoading()
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 登录成功后保存登录手机号，下次打开应用自动填充
				uni.setStorage({
					key: "login_number",
					data: this.phoneNumber
				})
				// this.$tab.redirectTo('./steps/index?stepsNumber=1')
				// this.$tab.reLaunch('/pages/index/index')
				// return
				// 认证情况
				authDetails().then(res => {
					let {
						data: {
							tcp = {}, // 嵌套默认值
							identity = {},
							data: data_auth = {},
							life_photos = {},
							vivid_photos = {},
							video = {},
						} = {}
					} = res || {} // 最外层也设置默认值
					let tcpStatus = tcp?.status
					let identityStatus = identity?.status ?? 2
					let dataStatus = data_auth?.status ?? 2
					let lifeStatus = life_photos?.status ?? 2
					let imageStatus = vivid_photos?.status ?? 2
					let videoStatus = video?.status ?? 2
					// 检查签署协议情况
					if (tcpStatus === 1) {
						this.$modal.closeLoading()
						// 已签署
						const statusList = [identityStatus, dataStatus, lifeStatus, imageStatus, videoStatus]
						let indexResule = statusList.indexOf(2)
						// 判断是否至少有一个环节为审核被驳回
						if (indexResule !== -1) {
							switch (indexResule) {
								case 0:
									// 实名认证被驳回
									this.$tab.redirectTo('./steps/index?stepsNumber=1')
									break;
								case 1:
									// 基本资料
									this.$tab.redirectTo('./steps/index?stepsNumber=2&current=1')
									break;
								case 2:
									// 形象照片
									this.$tab.redirectTo('./steps/index?stepsNumber=2&current=2')
									break;
								case 3:
									// 生活圈
									this.$tab.redirectTo('./steps/index?stepsNumber=2&current=3')
									break;
								case 4:
									// 生活视频
									this.$tab.redirectTo('./steps/index?stepsNumber=2&current=4')
									break;
								default:
									this.$tab.redirectTo('./steps/index?stepsNumber=3')
									break;
							}
						} else if (statusList.some(element => element === 0)) {
							// 判断是否至少有一个环节为审核中状态
							// 跳转至审核中页面
							this.$modal.closeLoading()
							this.$tab.redirectTo('./steps/index?stepsNumber=3')

						} else {
							// 进入首页
							this.$modal.closeLoading()
							this.$tab.reLaunch('/pages/index/index')
						}

					} else {
						// 未签署，跳转签署服务协议页
						this.$modal.closeLoading()
						this.$tab.redirectTo('merchant-agreement')
					}
				}).catch(() => this.$modal.closeLoading())
			},
			// 获取验证码
			getCode() {
				this.$modal.loading('正在发送').then(() => {
					getCode({
						mobile: this.phoneNumber
					}).then(res => {
						this.$modal.closeLoading()
						this.countdownStart()
					}).catch(() => this.$modal.closeLoading())
				})

			},
			countdownStart() {
				this.countdown = 60
				let countdown = setInterval(() => {
					if (this.countdown === 1) clearInterval(countdown)
					this.countdown -= 1
				}, 1000)
			}
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>